# @Version : 1.0
# @Author  : wxz
# @File    : user_schema.py
# @Time    : 2025/6/1 21:07
# @Describe: 描述...
import re

from pydantic import BaseModel, Field, field_validator


class UserRegister(BaseModel):

    username: str = Field(description="用户名")
    password: str = Field(description="密码")
    re_pwd: str = Field(description="确认密码")

    @field_validator('username')
    def username_validate(cls, username):
        if username.strip()== "":
            raise ValueError('用户名不能为空')

        return username

    # 校验密码格式
    @field_validator('password')
    def password_valid(cls, v):

        # 至少6位，包含数字和字母
        pattern = r'^(?=.*[a-zA-Z])(?=.*\d).{6,}$'

        match = re.match(pattern, v)
        if not match:
            raise ValueError('密码格式不匹配,至少6位，包含数字和字母')

        return v

class UserLogin(BaseModel):

    username: str = Field(description="用户名")
    password: str = Field(description="密码")

    @field_validator('username')
    def username_valid(cls, v):
        if v.strip()== "":
            raise ValueError('用户名不能为空')

        return v

    @field_validator('password')
    def password_validate(cls, password):
        if password.strip()== "":
            raise ValueError('密码不能为空')

        return password